package com.yoomiito.app.adapter.goods.share;

import android.graphics.Bitmap;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yoomiito.app.R;
import com.yoomiito.app.model.bean.SharePicInfo;
import com.yoomiito.app.widget.ImageView1_1;
import com.yoomiito.app.widget.SelectView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class GoodsPicShareAdapter extends BaseQuickAdapter<SharePicInfo, BaseViewHolder> {
    private List<SharePicInfo> data;
    private OnShowPicChange mOnShowPicChange;
    private List<String> urls = new ArrayList<>();
    private HashMap<Integer,Bitmap> mBitmaps = new HashMap();
    private List<Bitmap> mList = new ArrayList<>();
    private List<TextView> mTextViews = new ArrayList<>();
    public GoodsPicShareAdapter(@Nullable List<SharePicInfo> data) {
        super(R.layout.item_share_pic, data);
        this.data = data;
    }

    @Override
    protected void convert(final BaseViewHolder helper, SharePicInfo item) {
        final ImageView1_1 iv = helper.getView(R.id.item_share_iv);
        Glide.with(iv.getContext()).asBitmap().load(item.getPicUrl()).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                mBitmaps.put(helper.getLayoutPosition(),resource);
                iv.setImageBitmap(resource);
            }
        });
        final TextView tv = helper.getView(R.id.item_share_tv);
        mTextViews.add(tv);
        tv.setSelected(item.isShowPic());
        SelectView sv = helper.getView(R.id.item_share_sv);
        sv.setChangeListener((view, isSelected) -> {
            data.get(helper.getLayoutPosition()).setSelected(isSelected);
            if (isSelected){
                urls.add(data.get(helper.getLayoutPosition()).getPicUrl());
                mList.add(mBitmaps.get(helper.getLayoutPosition()));
            }else {
                if (urls.size()!=0){
                    urls.remove(data.get(helper.getLayoutPosition()).getPicUrl());
                }
                if (mList.size()!=0){
                    mList.remove(mBitmaps.get(helper.getLayoutPosition()));
                }

            }
        });
        sv.setSelect(item.isSelected());
        tv.setOnClickListener(v -> {//设置主题图
            onChange(helper.getLayoutPosition());
            if (mOnShowPicChange != null) {
                mOnShowPicChange.onChange(data.get(helper.getLayoutPosition()).getPicUrl());
            }
        });
    }

    public void setOnShowPicChange(OnShowPicChange onShowPicChange) {
        mOnShowPicChange = onShowPicChange;
    }

    public interface OnShowPicChange {
        void onChange(String url);
    }

    private void onChange(int position) {
        if (mTextViews.get(position).isSelected()){
            return;
        }
        for (int i = 0; i < mTextViews.size(); i++) {
            TextView tv = mTextViews.get(i);
            if (i == position) {
                tv.setSelected(true);
            }else if (tv.isSelected()) {
                tv.setSelected(false);
            }
        }
    }

    public List<String> getUrls() {
        return urls;
    }

    public List<Bitmap> getBitmaps(){ return mList;}

    public void clearBitmap(){
        mBitmaps = null;
        mList = null;
    }
}
